#!/bin/bash
#========================================================================
# Author: YSLin
# Email: 
# File Name: substitution.sh
# Description: 
#    1.Filename substitution (called globbing)
# 		* Matches zero or more occurrences of any character
# 		? Matches one occurrence of any character
# 		[characters] Matches one occurrence of any of the given characters
#       [!characters] Matches all charaters exclude any of the given characters
#       Within ‘[’ and ‘]’, character classes can be specified using the syntax [:class:], where class is one of the following classes defined in the posix standard:
#         alnum   alpha   ascii   blank   cntrl   digit   graph   lower
#         print   punct   space   upper   word    xdigit
#       ?(pattern-list) Matches zero or one occurrence of the given patterns. 
#       *(pattern-list) Matches zero or more occurrences of the given patterns. 
#       +(pattern-list) Matches one or more occurrences of the given patterns. 
#       @(pattern-list) Matches one of the given patterns. 
#       !(pattern-list) Matches anything except one of the given patterns.
#       
#    2.Value-based variable substitution
#		${parameter:-word} If parameter is null or unset, word is substituted for parameter. The value of parameter does not change.
#		${parameter:=word} If parameter is null or unset, parameter is set to the value of word.
#		${parameter:?message} If parameter is null or unset, message is printed to standard error. This checks that variables are set correctly.
#		${parameter:+word} If parameter is set, word is substituted for parameter. The value of parameter does not change.
#    3.Command substitution
# 		`command`
#    4.Arithmetic substitution
# 		/ The division operator. Divides two numbers and returns the result.
# 		* The multiplication operator. Multiples two numbers and returns the result.
# 		- The subtraction operator. Subtracts two numbers and returns the result.
# 		+ The addition operator. Adds two numbers and returns the result.
# 		() The parentheses clarify which expressions should be evaluated before others.
# Edit History: 
#   2010-12-17    File created.
#========================================================================

#==============================================================================
#1.Filename substitution (called globbing)
#==============================================================================
#* Matches zero or more occurrences of any character
#? Matches one occurrence of any character
#[characters] Matches one occurrence of any of the given characters

#Matching a File Prefix
#command prefix*
#$ ls ch1*

#Matching a File Suffix
#command *suffix
#$ ls *doc

#Matching Suffixes and Prefixes
#command prefix*suffix
#$ ls Backup*doc

#Matching Sets of Characters
#command [characters]
#$ ls ch0[0-9].doc
#ch01.doc ch02.doc ch03.doc ch04.doc ch05.doc ch06.doc

#$ ls [a-z]*
#$ ls *[a-zA-Z0-9]


#==============================================================================
#2.Value-based variable substitution
#==============================================================================
#${parameter:-word}
#PS1=${HOST:-localhost}"$ " ; export PS1 ;

#${parameter:=word}
#PS1=${HOST:=´uname -n´}"$ " ; export PS1 HOST ;
#上面的比下面的簡潔
#if [ -z "$HOST" ] ; then HOST=´uname -n´ ; fi ; PS1="$HOST$ "; export PS1 HOST ;

#${parameter:?message}
#當$HOME未設定時會顯示下面的訊息
: ${HOME:?"Your home directory is undefined."}

#${parameter:+word}
#當$DEBUG被設定過,就更改這個數值
DEBUG=none
echo ${DEBUG:+"Debug is active."}

#==============================================================================
#3.Command substitution
#==============================================================================
#`command`
#所有指令執行的結果都替換成左邊的變數
DATE=`date`
USERS=`who | wc -l`
UP=`date ; uptime`
echo "$DATE"
echo "$USERS"
echo "$UP"
#會先執行id -un然後替換成grep的參數
grep `id -un` /etc/passwd

#==============================================================================
#4.Arithmetic substitution
#==============================================================================
#$((expression))
foo=$(( ((5 + 3*2) - 4) / 2 ))
echo "foo is $foo"
foo=$((3 + $foo))
echo "foo is $foo"
